package com.googlecode.xapper;

import nu.xom.*;

public class HtmlElementWrapper {
	
	private static final String XHTML_NAMESPACE = "http://www.w3.org/1999/xhtml";
	
	public static final HtmlElementWrapper create(String tagName) {
		return new HtmlElementWrapper(new Element(tagName, XHTML_NAMESPACE));
	}
	
	public static final HtmlElementWrapper createPage() {
		return new HtmlElementWrapper(ReadyMades.page());
	}
	
	public static final HtmlElementWrapper element(Document aDocument) {
		return new HtmlElementWrapper(aDocument.getRootElement());
	}
	
	public static final HtmlElementWrapper element(Element element) {
		return new HtmlElementWrapper(element);
	}
	
	public HtmlElementWrapper withAttribute(String name, String value) {
		element.addAttribute(new Attribute(name, value));
		return this;
	}
	
	public Boolean hasAttribute(String name) {
		if(element.getAttribute(name) != null) {
			return Boolean.TRUE;
		}
		
		return Boolean.FALSE;
	}
	
	public HtmlElementWrapper withChild(String tagName) {
		Element child = new Element(tagName, XHTML_NAMESPACE);
		element.appendChild(child);
		element = child;
		return this;
	}
	
	public HtmlElementWrapper withSibling(String tagName) {
		Element root = (Element) element.getParent();
		
		if(root == null) {
			throw new IllegalStateException("A root element cannot have a sibling node");
		}
		
		Element sibling = new Element(tagName, XHTML_NAMESPACE);
		root.appendChild(sibling);
		
		element = sibling;
		
		return this;
	}
	
	public HtmlElementWrapper withText(String text) {
		element.appendChild(text);
		return this;
	}
	
	public HtmlElementWrapper parent() {
		Element parent = (Element) element.getParent();
		
		if(parent != null) {
			element = parent;
		}
		return this;
	}
	
	public HtmlElementWrapper root() {
		while((element.getParent()) != null) {
			element = (Element) element.getParent();
		}
		return this;
	}
	
	public HtmlElementWrapper withHeading(Integer level, String headingText) {
		Element heading = ReadyMades.heading(level, headingText);
		element.appendChild(heading);
		element = heading;
		return this;
	}
	
	public HtmlElementWrapper withParagraph() {
		Element paragraph = ReadyMades.paragraph();
		
		element.appendChild(paragraph);
		element = paragraph;
		return this;
	}
	
	public HtmlElementWrapper withDiv() {
		Element div = ReadyMades.div();
		element.appendChild(div);
		element = div;
		return this;
	}
	
	public HtmlElementWrapper withClass(String className) {
		element.addAttribute(new Attribute("class", className));
		return this;
	}
	
	public HtmlElementWrapper withId(String idValue) {
		element.addAttribute(new Attribute("id", idValue));
		return this;
	}

	private Element element;
	
	/**
	 * Returns the current wrapped element as a XOM Element
	 * @return The current wrapped element
	 */
	public Element asElement() { return element; }
	
	private HtmlElementWrapper(Element anElement) {
		element = anElement;
	}
}
